package com.itheima.controller;

import com.itheima.anno.Mylog;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.vendor;
import com.itheima.pojo.vendorQueryParam;
import com.itheima.service.vendorService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/vendor")
public class vendorController {

    @Autowired
    private vendorService vendorService;


    /**
     * 分页条件查询
     * @param vendorQueryParam
     * @return
     */
    @GetMapping
    public Result pageVendor( vendorQueryParam  vendorQueryParam) {
        log.info("vendorQueryParam : {}", vendorQueryParam);
        PageBean PageInfo =  vendorService.getpageInfo(vendorQueryParam);
        return Result.success(PageInfo);
    }
    /**
     * 新增供应商
     * @param vendor
     * @return
     */
    @Mylog
    @PostMapping
    public Result addVendor(@RequestBody vendor vendor) {
        log.info("新增供应商信息:{}", vendor);
        vendorService.addVendor(vendor);
        return Result.success();
    }
    /**
     * 删除供应商
     * @param ids
     * @return
     */
    @Mylog
    @DeleteMapping
    public Result deleteVendor(@RequestParam List<Integer> ids) {
        log.info("删除供应商id为:{}", ids);
        vendorService.deletevendor(ids);
        return Result.success();
    }

    /**
     * 查询所有供应商
     * @return
     */
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询所有的供应商数据");
        List<vendor> vendorList = vendorService.findAll();
        return Result.success(vendorList);
    }

    /**
     * 根据id查询供应商
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findByIdVendor(@PathVariable Integer id) {
        log.info("查询供应商id:{}", id);
        vendor v =  vendorService.findByidVendor(id);
        return Result.success(v);
    }

    /**
     * 修改供应商
     * @param vendor
     * @return
     */
    @Mylog
    @PutMapping
    public Result updateVendor(@RequestBody vendor vendor) {
        log.info("修改供应商信息:{}",vendor);
        vendorService.updateVendor(vendor);
        return Result.success();

    }


}
